package cn.learn.office.excel_v3.write;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 一行表头的设置
 *
 * @author huangyezhan
 * @version 3.0
 * @date 2021年05月21日18:59
 */
@Getter
@AllArgsConstructor
public class HeaderRow {

    private final List<HeaderCell> headerCells;


    /**
     * 每个单元格的设置
     */
    @Setter
    @Getter
    @Accessors(fluent = true)
    @NoArgsConstructor
    @AllArgsConstructor
    public static class HeaderCell {
        /**
         * 表头的名称（单个单元格）
         */
        private String name;

        /**
         * 行索引
         */
        private int rowIndex;

        /**
         * 列索引
         */
        private int colIndex;

        /**
         * 行占数
         */
        private int rowspan;
        /**
         * 列占数
         */
        private int colspan;
    }


    // ========================================以下是两个帮助创建表头的内部类========================================

    /**
     * 创建简单的单表头
     */
    public static SingleHeaderBuilder builderSingle() {
        return new SingleHeaderBuilder();
    }

    @Setter
    @Accessors(fluent = true)
    public static class SingleHeaderBuilder {
        private String[] name;

        private int rowIndex;

        public HeaderRow build() {
            List<HeaderCell> cells = new ArrayList<>();
            for (int i = 0; i < name.length; i++) {
                cells.add(
                        new HeaderCell()
                                .name(name[i])
                                .rowIndex(rowIndex)
                                .colIndex(i)
                                .rowspan(0)
                                .colspan(0)
                );
            }
            return new HeaderRow(cells);
        }

    }


    /**
     * 创建标准的表头
     */
    public static StandardHeaderBuilder builderStandard() {
        return new StandardHeaderBuilder();
    }

    /**
     * 标准表头，每次都要重新设置所有值
     */
    @Accessors(fluent = true)
    public static class StandardHeaderBuilder {
        private List<HeaderCell> headerCells;

        @Setter
        private String name;

        @Setter
        private int rowIndex;
        @Setter
        private int colIndex;
        @Setter
        private int rowspan;
        @Setter
        private int colspan;


        /**
         * 每设定一个单元格参数，就提交一次
         */
        public StandardHeaderBuilder commitCell() {
            if (null == headerCells) {
                headerCells = new ArrayList<>();
            }
            headerCells.add(new HeaderCell(name, rowIndex, colIndex, rowspan, colspan));
            return this;
        }

        public HeaderRow build() {
            Assert.notNull(headerCells, "确定headerCells数据不为空，请参考一下 commitCell()方法！");
            return new HeaderRow(headerCells);
        }
    }

}
